<template>
    <div>
        <h1>康复信息列表添加界面</h1>
      
        <table class="table">
            <tbody>
                <tr>
                    <td>自动生成编号</td>
                    <td>
                        <input type="text" v-model="data.rId">
                    </td>
                </tr>
                <tr>
                    <td>姓名</td>
                    <td>
                        <input type="text" v-model="data.name">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td>
                        <input type="radio" v-model="data.type" value="true" name="type">启用
                        <input type="radio"  v-model="data.type" value="false" name="type">禁用
                    </td>
                </tr>
                <tr>
                    <td>服务时长</td>
                    <td>
                        <input type="text" v-model="data.playTime">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="file" @change="Up">
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加" @click="Add">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import moment from 'moment';
import { onMounted } from 'vue';

onMounted(()=>{
    setcode();
})

const router=useRouter();
const data=ref({
    "id": 0,
  "rId": "string",
  "name": "",
  "isDeleted": false,
  "type": true,
  "playTime": 0
})

const setcode=()=>{
    //取随机数
    //取日期字符串
    var a=Math.random().toString().substring(2,5);
    var b=moment().format('YYYYMMDD');
    data.value.rId=a+b;
}

//导出
const Up=(a:any)=>{
    console.log(a.target.files[0].type);
}

const Add=()=>{
    if(data.value.name==""){
        alert('康复名称不能为空')
        return;
    }
    //服务时长为整数判定
    if(data.value.playTime*0!=0){
        alert('服务时长必须是整数判定')
        return;
    }



    axios.post("https://localhost:7178/api/Rehabilitation/Add",data.value).then(res=>{
        if(res.data>0){
            alert('添加成功');
            router.push({path:"/Show"});
        }else{
            alert('添加失败');
        }
    })


}

</script>

<style scoped>

</style>